package com.bootdo.essential.serivce.impl;

import com.bootdo.essential.domain.EssentialDO;
import com.bootdo.essential.serivce.EssentialService;
import com.bootdo.merchant.dao.AgentDao;
import com.bootdo.merchant.dao.MerchantDao;
import com.bootdo.merchant.domain.AgentDO;
import com.bootdo.merchant.domain.MerchantDO;
import com.bootdo.payee.dao.PayeeAgentDao;
import com.bootdo.payee.dao.PayeeDao;
import com.bootdo.payee.domain.PayeeAgentDO;
import com.bootdo.payee.domain.PayeeDO;
import com.bootdo.rate.dao.RateDao;
import com.bootdo.rate.domain.RateDO;
import com.bootdo.system.dao.UserDao;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bootdo.common.utils.ShiroUtils.getUserId;

/**
 * @Desc
 * @date 2019/12/23
 *
 */
@Service
public class EssentialServiceImpl implements EssentialService {

    @Autowired
    private UserRoleDao userRoleMapper;
    @Autowired
    private UserDao userMapper;
    @Autowired
    private AgentDao agentMapper;
    @Autowired
    private PayeeAgentDao payeeAgentMapper;
    @Autowired
    private MerchantDao merchantMapper;
    @Autowired
    private PayeeDao payeeMapper;
    @Autowired
    private RateDao rateMapper;


    @Override
    public EssentialDO list(){
        //获取用户权限
        EssentialDO essential = new EssentialDO();
        Long userId = getUserId();
        List<Long> list = userRoleMapper.listRoleId(userId);
        for (Long roleId : list){
            if (roleId == 60L){
                //商户代理
                AgentDO agent = agentMapper.get(userId);
                essential.setUserName(agent.getUserName());
                essential.setMobile(agent.getMobile());
                essential.setRoleId(roleId);
            } else if (roleId == 61L) {
                //收款代理
                PayeeAgentDO payeeAgent = payeeAgentMapper.get(userId);
                essential.setUserName(payeeAgent.getUserName());
                essential.setMobile(payeeAgent.getMobile());
                essential.setRoleId(roleId);
            } else if (roleId == 62L){
                //商户
                MerchantDO merchant = merchantMapper.get(userId);
                essential.setUserName(merchant.getUserName());
                essential.setMobile(merchant.getMobile());
                essential.setIdentification(merchant.getIdentification());
                essential.setToken(merchant.getToken());
                essential.setRoleId(roleId);
            } else if (roleId == 63L) {
                //收款户
                PayeeDO payee = payeeMapper.get(userId);
                essential.setUserName(payee.getUserName());
                essential.setMobile(payee.getMobile());
                essential.setRoleId(roleId);
            } else {
                //系统管理员
                UserDO user = userMapper.get(userId);
                essential.setUserName(user.getUsername());
                essential.setMobile(user.getMobile());
                essential.setRoleId(roleId);
            }
        }

        //获取用户费率
        RateDO rate = rateMapper.getRate(userId, 0);
        if (rate != null){
            essential.setAlipayFixedRate(rate.getAlipayFixedRate());
            essential.setWeixinFixedRate(rate.getWeixinFixedRate());
            essential.setAlipayTransferRate(rate.getAlipayTransferRate());
            essential.setAlipayAccountTransRate(rate.getAlipayAccountTransRate());
        }
        return essential;
    }
}
